package icasue.webdashboard.api;

import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import icasue.reflect.handles.HandleSupplier;
import icasue.reflect.handles.fields.FieldOF;
import icasue.webdashboard.apptree.buffer.FileMappingBuffer;

import java.lang.annotation.Annotation;
import java.net.URI;
import java.util.Set;
import java.util.function.Function;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/2/1 上午11:01
 * @UpdateDate:
 * @Description:
 */
public class DashboardApiWare {
    protected static String URI_TEMPLATE = "http://%s/%s/running-invoke/commonInvoke";
    protected static Class<? extends Annotation> methodMarker;
    protected static HandleSupplier.PredicateAry filterMethod;
    protected static Function<String,Object> getCacheData;
    protected static HandleSupplier.ConsumerAry setCacheData;
    protected static final RestTemplate restTemplate = new RestTemplate();
    protected static URI localAppURI;
    protected static String localNode;
    protected static String appName;
    protected static HttpHeaders postHandlers;
    protected static final String EXEC_SUCCESS = "SUCCESS";
    protected static final String LOCATE_CLASS_ERROR = "LOCATE_CLASS_ERROR";
    protected static final String SERVER_API_ERROR = "LOCAL API EXECUTE ERROR : %s";
    //This client using for execute icasue.runninginvoke.task on all instance which user given.
    protected static Object discoveryClient;
    protected static Function<String,Set<String>> clusterModeServerIdentityTree;

    static {
        filterMethod = (HandleSupplier.PredicateAry) FieldOF.get_ofTp_fName_instance_.apply(FileMappingBuffer.class, "filterMethod", null);
        getCacheData = (Function<String,Object>)FieldOF.get_ofTp_fName_instance_.apply(FileMappingBuffer.class, "getCacheData", null);
        setCacheData = (HandleSupplier.ConsumerAry)FieldOF.get_ofTp_fName_instance_.apply(FileMappingBuffer.class, "setCacheData", null);
        try {
            methodMarker = (Class<? extends Annotation>)Class.forName("icasue.runninginvoke.annotations.RunningInvokable");
        }catch (Throwable e){ /*Method marker not exist, means icasue-plugins-running-invoke not be used.*/}
    }
}
